//
//  MyViewController.m
//  MyPaintBoard
//
//  Created by 黄珂耀 on 15/8/6.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "MyViewController.h"
#import "ColorCell.h"
#import "LineWidthCell.h"
#import "MyPaintBoardView.h"

#define KWidth [UIScreen mainScreen].bounds.size.width
#define KHeight [UIScreen mainScreen].bounds.size.height

@interface MyViewController ()
{
    BOOL _isColor;
    
    UILabel *_indication;
    
    MyPaintBoardView *_myPaintBoardView;
    
    CGPoint _myBeginPoint;
    
    CGPoint _myMovePoint;
    
    UIColor *_currentColor;
    
    CGFloat _currentLineWidth;
}

@end

@implementation MyViewController


#pragma mark - DefaultOpertion

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.652 alpha:1.000];
    
    [self _loadData];
    
    [self _createButtons];
    
    [self _createColorCollectionView];
    
    [self _createMyPaintBoardView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData

- (void)_loadData
{
    _isColor = YES;
    
    _currentColor = [UIColor blackColor];
    _currentLineWidth = 3.0;
    
    _colorArray = @[[UIColor colorWithWhite:0.357 alpha:1.000],[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor brownColor],[UIColor blackColor]];\
    
    _color = [[UIButton alloc] init];
    _lineWidth = [[UIButton alloc] init];
    _eraser = [[UIButton alloc] init];
    _cancel = [[UIButton alloc] init];
    _clear = [[UIButton alloc] init];
    
    _buttons = @[_color,_lineWidth,_eraser,_cancel,_clear];
    
    _myButtons = [[NSMutableArray alloc] initWithArray:_buttons];
    
    _buttonNames = @[@"颜色",@"线宽",@"橡皮",@"撤销",@"清屏"];
    
    _lineWidthNames = @[@"1点",@"3点",@"5点",@"8点",@"10点",@"15点",@"20点"];
    
    _lineWidthArray = @[@1.0,@3.0,@5.0,@8.0,@10.0,@15.0,@20.0];
}

#pragma mark - Actions

- (void)colorAction
{
    if (_isColor)
    {
        return;
    }
    else
    {
        [self _createColorCollectionView];
        
        _isColor = !_isColor;
    }

    _indication.frame = CGRectMake(0, 60, 75, 2);
    [self.view addSubview:_indication];
}

- (void)lineWidthAction
{
    if (_isColor)
    {
        [self _createLineWidthCollectionView];
        _isColor = !_isColor;
    }
    else
    {
        [self _createLineWidthCollectionView];
    }

    _indication.frame = CGRectMake(75, 60, 75, 2);
    [self.view addSubview:_indication];

}

- (void)eraserAction
{
    _currentColor = [UIColor whiteColor];

    _indication.frame = CGRectMake(150, 60, 75, 2);
    [self.view addSubview:_indication];

}

- (void)cancelAction
{
    [_myPaintBoardView cancelAction];

    _indication.frame = CGRectMake(225, 60, 75, 2);
    [self.view addSubview:_indication];

}

- (void)clearAction
{
    [_myPaintBoardView clearAction];
    
    _indication.frame = CGRectMake(300, 60, 75, 2);
    [self.view addSubview:_indication];
}

#pragma mark - CreateSubviews

- (void)_createButtons
{
    
    CGFloat buttonWidth = KWidth / 5;
    CGFloat buttonHeight = 40;
    
    for (int i = 0; i < 5; i++)
    {
        _myButtons[i] = [[UIButton alloc] initWithFrame:CGRectMake(i * buttonWidth, 20, buttonWidth, buttonHeight)];
        
        [_myButtons[i] setTitle:_buttonNames[i] forState:UIControlStateNormal];
        
        [_myButtons[i] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.view addSubview:_myButtons[i]];
    }
    
    [_myButtons[0] addTarget:self action:@selector(colorAction) forControlEvents:UIControlEventTouchUpInside];
    [_myButtons[1] addTarget:self action:@selector(lineWidthAction) forControlEvents:UIControlEventTouchUpInside];
    [_myButtons[2] addTarget:self action:@selector(eraserAction) forControlEvents:UIControlEventTouchUpInside];
    [_myButtons[3] addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_myButtons[4] addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)_createColorCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(KWidth / 10, 60);
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumInteritemSpacing = 0;
    
    layout.minimumLineSpacing = 4;
    
    _colorCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(3, 60, KWidth, 80) collectionViewLayout:layout];
    
    [self.view addSubview:_colorCollectionView];
    
    [_colorCollectionView registerClass:[ColorCell class] forCellWithReuseIdentifier:@"colorCell"];
    
    _colorCollectionView.delegate = self;
    
    _colorCollectionView.dataSource = self;
    
    _colorCollectionView.backgroundColor = [UIColor colorWithWhite:0.652 alpha:1.000];
    
    _indication = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 75, 2)];
    _indication.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:_indication];
    
}

- (void)_createLineWidthCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(KWidth / 8, 60);
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumInteritemSpacing = 0;
    
    layout.minimumLineSpacing = 7;
    
    _lineWidthCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, KWidth, 80) collectionViewLayout:layout];
    
    [self.view addSubview:_lineWidthCollectionView];
    
    [_lineWidthCollectionView registerClass:[LineWidthCell class] forCellWithReuseIdentifier:@"lineWidthCell"];
    
    _lineWidthCollectionView.delegate = self;
    
    _lineWidthCollectionView.dataSource = self;
    
    _lineWidthCollectionView.backgroundColor = [UIColor colorWithWhite:0.652 alpha:1.000];
    
}

- (void)_createMyPaintBoardView
{
    _myPaintBoardView = [[MyPaintBoardView alloc] initWithFrame:CGRectMake(0, 140, KWidth, KHeight - 140)];
    
    _myPaintBoardView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_myPaintBoardView];
}

#pragma mark - ColorDelegates

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_isColor)
    {
        return [_colorArray count];
    }
    else
    {
        return [_lineWidthArray count];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isColor)
    {
        ColorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colorCell" forIndexPath:indexPath];
        
        cell.cellColor = _colorArray[indexPath.row];
        
        return cell;
    }else
    {
        LineWidthCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lineWidthCell" forIndexPath:indexPath];
        
        cell.lineWidthName = _lineWidthNames[indexPath.row];
        
        cell.lineWidth = [_lineWidthArray[indexPath.row] floatValue];
        
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isColor)
    {
        ColorCell *cell = [[ColorCell alloc] init];
        cell = (ColorCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        _currentColor = cell.cellColor;
        
    }
    else
    {
        LineWidthCell *cell = [[LineWidthCell alloc] init];
        cell = (LineWidthCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        _currentLineWidth = cell.lineWidth;
    }
}

#pragma mark - TouchActions

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    _myBeginPoint = [touch locationInView:_myPaintBoardView];
    
    _myPaintBoardView.color = _currentColor;
    
    _myPaintBoardView.lineWidth = _currentLineWidth;
    
    [_myPaintBoardView addColor];
    [_myPaintBoardView addLineWidth];
    [_myPaintBoardView initWithMyAllPoints];
    [_myPaintBoardView setPoint:_myBeginPoint];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSArray *movePointArray=[touches allObjects];
    _myMovePoint = [[movePointArray objectAtIndex:0] locationInView:_myPaintBoardView];
    
    [_myPaintBoardView setPoint:_myMovePoint];
    
    [_myPaintBoardView setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_myPaintBoardView addLine];
    [_myPaintBoardView setNeedsDisplay];
}


@end
